clear
*** use "***\KQS_2021_w21.dta


des

mvdecode kp*, mv(0 71)


*** Wahlentscheidung ***

fre kp20_180
fre kp20_200b kp20_191b
gen wv=0 if kp20_180==2
replace wv=1 if kp20_200b==1 | kp20_191b==1
replace wv=4 if kp20_200b==4 | kp20_191b==4
replace wv=5 if kp20_200b==5 | kp20_191b==5
replace wv=6 if kp20_200b==6 | kp20_191b==6
replace wv=7 if kp20_200b==7 | kp20_191b==7
replace wv=8 if kp20_200b==322 | kp20_191b==322
replace wv=9 if kp20_200b==801 | kp20_191b==801
fre wv

*** Politikerbewertungen

gen merkel=(kp20_650a-1)/10
gen laschet=(kp20_650b1-1)/10
gen baerbock=(kp20_650f2-1)/10
gen scholz=(kp20_650o1-1)/10
gen weidel=(kp20_650h2-1)/10
gen lindner=(kp20_650s-1)/10
gen bartsch=(kp20_650g2-1)/10
gen soeder=(kp20_650k1-1)/10



*** Bewertung der Regierungsleistung ***

fre kp20_730
gen bureg=(kp20_730-6)/5
gen bureg_cdu=(kp20_731a-6)/5
gen bureg_csu=(kp20_731b-6)/5
gen bureg_spd=(kp20_731c-6)/5
fre bureg*

*** pandemiespezifisch ***

fre kp20_4046
gen bureg_pan=(kp20_4046-6)/5
fre bureg_pan


*** Parteiidentifikation

fre kp20_2090
gen pid=kp20_2090 
recode pid (1 2 3=1)
gen unionpid=0 if pid>0 & pid<900
replace unionpid=1 if pid==1
gen spdpid=0 if pid>0 & pid<900
replace spdpid=1 if pid==4
gen fdppid=0 if pid>0 & pid<900
replace fdppid=1 if pid==5
gen grupid=0 if pid>0 & pid<900
replace grupid=1 if pid==6
gen linpid=0 if pid>0 & pid<900
replace linpid=1 if pid==7
gen afdpid=0 if pid>0 & pid<900
replace afdpid=1 if pid==322
fre afdpid



fre pid

*** Parteikompetenzen *******

gen unionk1=0 if kp20_850>0 & kp20_850<900
replace unionk1=1 if kp20_850==1 | kp20_850==2 | kp20_850==3
gen spdk1=0 if kp20_850>0 & kp20_850<900
replace spdk1=1 if kp20_850==4
gen fdpk1=0 if kp20_850>0 & kp20_850<900
replace fdpk1=1 if kp20_850==5
gen gruk1=0 if kp20_850>0 & kp20_850<900
replace gruk1=1 if kp20_850==6
gen link1=0 if kp20_850>0 & kp20_850<900
replace link1=1 if kp20_850==7
gen afdk1=0 if kp20_850>0 & kp20_850<900
replace afdk1=1 if kp20_850==322

gen unionk2=0 if kp20_870>0 & kp20_870<900
replace unionk2=1 if kp20_870==1 | kp20_870==2 | kp20_870==3
gen spdk2=0 if kp20_870>0 & kp20_870<900
replace spdk2=1 if kp20_870==4
gen fdpk2=0 if kp20_870>0 & kp20_870<900
replace fdpk2=1 if kp20_870==5
gen gruk2=0 if kp20_870>0 & kp20_870<900
replace gruk2=1 if kp20_870==6
gen link2=0 if kp20_870>0 & kp20_870<900
replace link2=1 if kp20_870==7
gen afdk2=0 if kp20_870>0 & kp20_870<900
replace afdk2=1 if kp20_870==322

gen unionk=(unionk1+unionk2)/2
gen spdk=(spdk1+spdk2)/2
gen fdpk=(fdpk1-fdpk2)/2
gen gruk=(gruk1-gruk2)/2
gen link=(link1-link2)/2
gen afdk=(afdk1-afdk2)/2
fre unionk-afdk

*** Policy-Orientierungen ***

fre kp20_1590
gen gesch=(kp20_1590-1)/6
gen klima=(kp20_1290-1)/6
gen socec=(kp20_1090-1)/6
gen zuzug=(kp20_1130-1)/6
fre zuzug


*** Grundlage für Abbildung 4 ***

mean bureg, over (pid)
mean bureg_pan, over (pid)
mean bureg_cdu, over (pid)
mean bureg_spd, over (pid)
mean bureg_csu, over (pid)


*** Abbildung 5 ***


matrix bureg1 = J(7,3,.) // 8 rows, 3 columns 

mlogit wv bureg gesch klima socec zuzug unionpid spdpid fdppid grupid linpid afdpid if wv<9
eststo m1a: margins, dydx (bureg) post

mat r=r(table)
mat list r(table)
matrix bureg1[1, 1] = r[1,2]
matrix bureg1[1, 2] = r[5,2]
matrix bureg1[1, 3] = r[6,2]
matrix bureg1[2, 1] = r[1,3]
matrix bureg1[2, 2] = r[5,3]
matrix bureg1[2, 3] = r[6,3]
matrix bureg1[3, 1] = r[1,7]
matrix bureg1[3, 2] = r[5,7]
matrix bureg1[3, 3] = r[6,7]
matrix bureg1[4, 1] = r[1,4]
matrix bureg1[4, 2] = r[5,4]
matrix bureg1[4, 3] = r[6,4]
matrix bureg1[5, 1] = r[1,6]
matrix bureg1[5, 2] = r[5,6]
matrix bureg1[5, 3] = r[6,6]
matrix bureg1[6, 1] = r[1,5]
matrix bureg1[6, 2] = r[5,5]
matrix bureg1[6, 3] = r[6,5]
matrix bureg1[7, 1] = r[1,1]
matrix bureg1[7, 2] = r[5,1]
matrix bureg1[7, 3] = r[6,1]
matlist bureg1


matrix bureg2 = J(7,3,.) // 8 rows, 3 columns 

mlogit wv bureg gesch klima socec zuzug unionpid spdpid fdppid grupid linpid afdpid merkel laschet soeder scholz baerbock lindner if wv<9
eststo m2a: margins, dydx (bureg) post

mat r=r(table)
mat list r(table)
matrix bureg2[1, 1] = r[1,2]
matrix bureg2[1, 2] = r[5,2]
matrix bureg2[1, 3] = r[6,2]
matrix bureg2[2, 1] = r[1,3]
matrix bureg2[2, 2] = r[5,3]
matrix bureg2[2, 3] = r[6,3]
matrix bureg2[3, 1] = r[1,7]
matrix bureg2[3, 2] = r[5,7]
matrix bureg2[3, 3] = r[6,7]
matrix bureg2[4, 1] = r[1,4]
matrix bureg2[4, 2] = r[5,4]
matrix bureg2[4, 3] = r[6,4]
matrix bureg2[5, 1] = r[1,6]
matrix bureg2[5, 2] = r[5,6]
matrix bureg2[5, 3] = r[6,6]
matrix bureg2[6, 1] = r[1,5]
matrix bureg2[6, 2] = r[5,5]
matrix bureg2[6, 3] = r[6,5]
matrix bureg2[7, 1] = r[1,1]
matrix bureg2[7, 2] = r[5,1]
matrix bureg2[7, 3] = r[6,1]
matlist bureg2


matrix bureg3 = J(7,3,.) // 8 rows, 3 columns 

mlogit wv bureg_pan gesch klima socec zuzug unionpid spdpid fdppid grupid linpid afdpid if wv<9
eststo m3a: margins, dydx (bureg_pan) post

mat r=r(table)
mat list r(table)
matrix bureg3[1, 1] = r[1,2]
matrix bureg3[1, 2] = r[5,2]
matrix bureg3[1, 3] = r[6,2]
matrix bureg3[2, 1] = r[1,3]
matrix bureg3[2, 2] = r[5,3]
matrix bureg3[2, 3] = r[6,3]
matrix bureg3[3, 1] = r[1,7]
matrix bureg3[3, 2] = r[5,7]
matrix bureg3[3, 3] = r[6,7]
matrix bureg3[4, 1] = r[1,4]
matrix bureg3[4, 2] = r[5,4]
matrix bureg3[4, 3] = r[6,4]
matrix bureg3[5, 1] = r[1,6]
matrix bureg3[5, 2] = r[5,6]
matrix bureg3[5, 3] = r[6,6]
matrix bureg3[6, 1] = r[1,5]
matrix bureg3[6, 2] = r[5,5]
matrix bureg3[6, 3] = r[6,5]
matrix bureg3[7, 1] = r[1,1]
matrix bureg3[7, 2] = r[5,1]
matrix bureg3[7, 3] = r[6,1]
matlist bureg3


matrix bureg4 = J(7,3,.) // 8 rows, 3 columns 

mlogit wv bureg_pan gesch klima socec zuzug unionpid spdpid fdppid grupid linpid afdpid merkel laschet soeder scholz baerbock lindner if wv<9
eststo m4a: margins, dydx (bureg) post

mat r=r(table)
mat list r(table)
matrix bureg4[1, 1] = r[1,2]
matrix bureg4[1, 2] = r[5,2]
matrix bureg4[1, 3] = r[6,2]
matrix bureg4[2, 1] = r[1,3]
matrix bureg4[2, 2] = r[5,3]
matrix bureg4[2, 3] = r[6,3]
matrix bureg4[3, 1] = r[1,7]
matrix bureg4[3, 2] = r[5,7]
matrix bureg4[3, 3] = r[6,7]
matrix bureg4[4, 1] = r[1,4]
matrix bureg4[4, 2] = r[5,4]
matrix bureg4[4, 3] = r[6,4]
matrix bureg4[5, 1] = r[1,6]
matrix bureg4[5, 2] = r[5,6]
matrix bureg4[5, 3] = r[6,6]
matrix bureg4[6, 1] = r[1,5]
matrix bureg4[6, 2] = r[5,5]
matrix bureg4[6, 3] = r[6,5]
matrix bureg4[7, 1] = r[1,1]
matrix bureg4[7, 2] = r[5,1]
matrix bureg4[7, 3] = r[6,1]
matlist bureg4



grstyle init
	grstyle set imesh, horizontal 
	coefplot (matrix(bureg1[,1]), ci((bureg1[,2] bureg1[,3])) msymbol(circle) mcolor(gs0) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(0.25)) ///
	(matrix(bureg2[,1]), ci((bureg2[,2] bureg2[,3])) msymbol(circle) mcolor(gs6) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(0.08)) ///
		(matrix(bureg3[,1]), ci((bureg3[,2] bureg3[,3])) msymbol(circle) mcolor(gs13) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(-0.08)) ///
			(matrix(bureg4[,1]), ci((bureg4[,2] bureg4[,3])) msymbol(circle) mcolor(gs16) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(-0.25)), /// 
			coeflabels(r1="CDU/CSU" r2="SPD" r3="AfD" r4="FDP" r5="Die Linke" r6="Grüne" r7="Nichtwahl", labsize(large) labcolor(gs0)) ///
			xscale(range(-.6 .6)) xlabel(-.6 (.2) .6, noticks labsize(medium) labcolor(gs0)) xmtick(none) ///
		legend(order(2 "Regierung" 4 "Regierung (kons.)" 6 "Pandemie" 8 "Pandemie (kons.)") position(6) col (2) size(large) symxsize(5) region(lcolor(gs16)))  /// 
			plotregion(color(gs15)) ///
			graphregion(margin(-6 0 -5 0)) ysize(2.5) xsize(3)  /// scale(.8)
			name(fig2a, replace)


			
matrix bureg5 = J(7,3,.) // 8 rows, 3 columns 

mlogit wv bureg_cdu bureg_spd bureg_csu gesch klima socec zuzug unionpid spdpid fdppid grupid linpid afdpid if wv<9
eststo m5a: margins, dydx (bureg_cdu) post

mat r=r(table)
mat list r(table)
matrix bureg5[1, 1] = r[1,2]
matrix bureg5[1, 2] = r[5,2]
matrix bureg5[1, 3] = r[6,2]
matrix bureg5[2, 1] = r[1,3]
matrix bureg5[2, 2] = r[5,3]
matrix bureg5[2, 3] = r[6,3]
matrix bureg5[3, 1] = r[1,7]
matrix bureg5[3, 2] = r[5,7]
matrix bureg5[3, 3] = r[6,7]
matrix bureg5[4, 1] = r[1,4]
matrix bureg5[4, 2] = r[5,4]
matrix bureg5[4, 3] = r[6,4]
matrix bureg5[5, 1] = r[1,6]
matrix bureg5[5, 2] = r[5,6]
matrix bureg5[5, 3] = r[6,6]
matrix bureg5[6, 1] = r[1,5]
matrix bureg5[6, 2] = r[5,5]
matrix bureg5[6, 3] = r[6,5]
matrix bureg5[7, 1] = r[1,1]
matrix bureg5[7, 2] = r[5,1]
matrix bureg5[7, 3] = r[6,1]
matlist bureg5

matrix bureg6 = J(7,3,.) // 8 rows, 3 columns 

mlogit wv bureg_cdu bureg_spd bureg_csu gesch klima socec zuzug unionpid spdpid fdppid grupid linpid afdpid merkel laschet soeder scholz baerbock lindner if wv<9
eststo m6a: margins, dydx (bureg_cdu) post

mat r=r(table)
mat list r(table)
matrix bureg6[1, 1] = r[1,2]
matrix bureg6[1, 2] = r[5,2]
matrix bureg6[1, 3] = r[6,2]
matrix bureg6[2, 1] = r[1,3]
matrix bureg6[2, 2] = r[5,3]
matrix bureg6[2, 3] = r[6,3]
matrix bureg6[3, 1] = r[1,7]
matrix bureg6[3, 2] = r[5,7]
matrix bureg6[3, 3] = r[6,7]
matrix bureg6[4, 1] = r[1,4]
matrix bureg6[4, 2] = r[5,4]
matrix bureg6[4, 3] = r[6,4]
matrix bureg6[5, 1] = r[1,6]
matrix bureg6[5, 2] = r[5,6]
matrix bureg6[5, 3] = r[6,6]
matrix bureg6[6, 1] = r[1,5]
matrix bureg6[6, 2] = r[5,5]
matrix bureg6[6, 3] = r[6,5]
matrix bureg6[7, 1] = r[1,1]
matrix bureg6[7, 2] = r[5,1]
matrix bureg6[7, 3] = r[6,1]
matlist bureg6

matrix bureg7 = J(7,3,.) // 8 rows, 3 columns 

mlogit wv bureg_cdu bureg_spd bureg_csu gesch klima socec zuzug unionpid spdpid fdppid grupid linpid afdpid if wv<9
eststo m7a: margins, dydx (bureg_spd) post

mat r=r(table)
mat list r(table)
matrix bureg7[1, 1] = r[1,2]
matrix bureg7[1, 2] = r[5,2]
matrix bureg7[1, 3] = r[6,2]
matrix bureg7[2, 1] = r[1,3]
matrix bureg7[2, 2] = r[5,3]
matrix bureg7[2, 3] = r[6,3]
matrix bureg7[3, 1] = r[1,7]
matrix bureg7[3, 2] = r[5,7]
matrix bureg7[3, 3] = r[6,7]
matrix bureg7[4, 1] = r[1,4]
matrix bureg7[4, 2] = r[5,4]
matrix bureg7[4, 3] = r[6,4]
matrix bureg7[5, 1] = r[1,6]
matrix bureg7[5, 2] = r[5,6]
matrix bureg7[5, 3] = r[6,6]
matrix bureg7[6, 1] = r[1,5]
matrix bureg7[6, 2] = r[5,5]
matrix bureg7[6, 3] = r[6,5]
matrix bureg7[7, 1] = r[1,1]
matrix bureg7[7, 2] = r[5,1]
matrix bureg7[7, 3] = r[6,1]
matlist bureg7

matrix bureg8 = J(7,3,.) // 8 rows, 3 columns 

mlogit wv bureg_cdu bureg_spd bureg_csu gesch klima socec zuzug unionpid spdpid fdppid grupid linpid afdpid merkel laschet soeder scholz baerbock lindner if wv<9
eststo m8a: margins, dydx (bureg_spd) post

mat r=r(table)
mat list r(table)
matrix bureg8[1, 1] = r[1,2]
matrix bureg8[1, 2] = r[5,2]
matrix bureg8[1, 3] = r[6,2]
matrix bureg8[2, 1] = r[1,3]
matrix bureg8[2, 2] = r[5,3]
matrix bureg8[2, 3] = r[6,3]
matrix bureg8[3, 1] = r[1,7]
matrix bureg8[3, 2] = r[5,7]
matrix bureg8[3, 3] = r[6,7]
matrix bureg8[4, 1] = r[1,4]
matrix bureg8[4, 2] = r[5,4]
matrix bureg8[4, 3] = r[6,4]
matrix bureg8[5, 1] = r[1,6]
matrix bureg8[5, 2] = r[5,6]
matrix bureg8[5, 3] = r[6,6]
matrix bureg8[6, 1] = r[1,5]
matrix bureg8[6, 2] = r[5,5]
matrix bureg8[6, 3] = r[6,5]
matrix bureg8[7, 1] = r[1,1]
matrix bureg8[7, 2] = r[5,1]
matrix bureg8[7, 3] = r[6,1]
matlist bureg8



grstyle init
	grstyle set imesh, horizontal 
	coefplot (matrix(bureg5[,1]), ci((bureg5[,2] bureg5[,3])) msymbol(circle) mcolor(gs0) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(0.25)) ///
	(matrix(bureg6[,1]), ci((bureg6[,2] bureg6[,3])) msymbol(circle) mcolor(gs6) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(0.08)) ///
		(matrix(bureg7[,1]), ci((bureg7[,2] bureg7[,3])) msymbol(circle) mcolor(gs13) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(-0.08)) ///
			(matrix(bureg8[,1]), ci((bureg8[,2] bureg8[,3])) msymbol(circle) mcolor(gs16) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(-0.25)), /// 
			coeflabels(r1="CDU/CSU" r2="SPD" r3="AfD" r4="FDP" r5="Die Linke" r6="Grüne" r7="Nichtwahl", labsize(large) labcolor(gs0)) ///
			xscale(range(-.6 .6)) xlabel(-.6 (.2) .6, noticks labsize(medium) labcolor(gs0)) xmtick(none) ///
		legend(order(2 "CDU" 4 "CDU (kons.)" 6 "SPD" 8 "SPD (kons.)") position(6) col(2)  size(small) symxsize(5) region(lcolor(gs16)))  /// 
			plotregion(color(gs15)) ///
			graphregion(margin(-6 0 -5 0)) ysize(2.5) xsize(3)  /// scale(.8)
			name(fig2b, replace)

